<template>
    <div class="slider-container">
        <div class="slider">
            <div class="slide banner1">
                <div class="date">{{ peakTime.peakDay }} {{ peakTime.peakPeriod }}</div>
                <div class="week">{{ peakTime.weekday }}-{{ peakTime.maxCount }}人</div>
                <div class="line"></div>
                <div class="weekend">
                    <div class="weeks odd">
                        <div>
                            <div class="weekday " style="color:#58A5FC">周一</div>
                            <div class="time" v-for="(value, key, index) in weekdayPeriod['周一'].period"
                                :class="index == 3 ? '' : 'bottom-line'" :key="key">
                                <span>{{ key }}</span>
                                <div><span :class="weekdayPeriod['周一'].maxCount == key ? 'max-num' : ''">{{ value
                                }}</span><span>人</span>
                                </div>
                                <img src="../../../assets/max.png" alt="" class="max"
                                    v-if="weekdayPeriod['周一'].maxCount == key">
                            </div>
                        </div>

                    </div>
                    <div class="weeks">
                        <div>
                            <div class="weekday" style="color:#B495FC">周二</div>
                            <div class="time" v-for="(value, key, index) in weekdayPeriod['周二'].period"
                                :class="index == 3 ? '' : 'bottom-line'" :key="key">
                                <span>{{ key }}</span>
                                <div><span :class="weekdayPeriod['周二'].maxCount == key ? 'max-num' : ''">{{ value
                                }}</span><span>人</span>
                                </div>
                                <img src="../../../assets/max.png" alt="" class="max"
                                    v-if="weekdayPeriod['周二'].maxCount == key">
                            </div>
                        </div>

                    </div>
                    <div class="weeks odd">
                        <div>
                            <div class="weekday " style="color:#88C328">周三</div>
                            <div class="time" v-for="(value, key, index) in weekdayPeriod['周三'].period"
                                :class="index == 3 ? '' : 'bottom-line'" :key="key">
                                <span>{{ key }}</span>
                                <div><span :class="weekdayPeriod['周三'].maxCount == key ? 'max-num' : ''">{{ value
                                }}</span><span>人</span>
                                </div>
                                <img src="../../../assets/max.png" alt="" class="max"
                                    v-if="weekdayPeriod['周三'].maxCount == key">
                            </div>
                        </div>

                    </div>
                    <div class="weeks">
                        <div>
                            <div class="weekday" style="color:#FF6666">周四</div>
                            <div class="time" v-for="(value, key, index) in weekdayPeriod['周四'].period"
                                :class="index == 3 ? '' : 'bottom-line'" :key="key">
                                <span>{{ key }}</span>
                                <div><span :class="weekdayPeriod['周四'].maxCount == key ? 'max-num' : ''">{{ value
                                }}</span><span>人</span>
                                </div>
                                <img src="../../../assets/max.png" alt="" class="max"
                                    v-if="weekdayPeriod['周四'].maxCount == key">
                            </div>
                        </div>

                    </div>
                    <div class="weeks odd">
                        <div>
                            <div class="weekday " style="color:#C5B272">周五</div>
                            <div class="time" v-for="(value, key, index) in weekdayPeriod['周五'].period"
                                :class="index == 3 ? '' : 'bottom-line'" :key="key">
                                <span>{{ key }}</span>
                                <div><span :class="weekdayPeriod['周五'].maxCount == key ? 'max-num' : ''">{{ value
                                }}</span><span>人</span>
                                </div>
                                <img src="../../../assets/max.png" alt="" class="max"
                                    v-if="weekdayPeriod['周五'].maxCount == key">
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>

import { flowPeak, weekPeak } from '@/api/statistics';

export default {
    name: "weeklyTrafficStatistics",  //一周流量统计
    components: {
    },
    props: {
        month: {
            type: String,
            // required: true,
        }
    },
    data() {
        return {
            peakTime: {
                peakDay: '',
                peakPeriod: '',
                maxCount: '',
                weekday: ''
            },
            weekdayPeriod: {
                "周一": {},
                "周二": {},
                "周三": {},
                "周四": {},
                "周五": {}
            },
            timeArr: {
                "9:00-11:00": 99,
                "11:00-13:00": 28,
                "13:00-15:00": 50,
                "15:00-17:00": 97
            },
            //  [{
            //     time: "9:00-11:00",
            //     count: 99
            // }, {
            //     time: "11:00-13:00",
            //     count: 28
            // }, {
            //     time: "13:00-15:00",
            //     count: 50
            // }, {
            //     time: "15:00-17:00",
            //     count: 97
            // }]
        };
    },
    watch: {
        month(val) {
            // console.log('month', val);
            this.nowMonth = val;
            this.getPeakTime();
            this.getweekDayPeriod();
        }
    },
    created() {
        // this.getPeakTime();
        // this.getweekDayPeriod();
    },
    mounted() {

    },
    methods: {
        getPeakTime() {
            let query = {
                month: this.nowMonth
            }
            flowPeak(query).then(res => {
                if (res.code === 200) {
                    const dateNumber = res.data.peakDay;
                    const dateStr = dateNumber.toString();
                    const year = dateStr.substring(0, 4);
                    const month = (parseInt(dateStr.substring(4, 6), 10)).toString() // 月份加1
                    const day = parseInt(dateStr.substring(6, 8)).toString()
                    console.log(year, month, day, '年月日');
                    const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
                    const dayOfWeek = weekDays[new Date(year, month - 1, day).getDay()];
                    this.peakTime.weekday = dayOfWeek;
                    // 拼接成YYYY-MM-DD格式的字符串
                    this.peakTime.peakDay = `${year}-${month}-${day}`
                    this.peakTime.maxCount = res.data.maxCount;
                    this.peakTime.peakPeriod = res.data.peakPeriod.split('-')[0] + '：00-' + res.data.peakPeriod.split('-')[1] + '：00';
                }
            })
        },
        getweekDayPeriod() {
            let query = {
                month: this.nowMonth
            }
            weekPeak(query).then(res => {
                if (res.code === 200) {
                    const data = res.data;
                    this.weekdayPeriod["周一"].period = this.formatPeriod(data["MONDAY"])
                    console.log(this.getPropertyWithMaxValue(data["MONDAY"]), '最大');
                    this.weekdayPeriod["周一"].maxCount = this.getPropertyWithMaxValue(this.weekdayPeriod["周一"].period).property;
                    console.log(this.weekdayPeriod, 'weekdayPeriod');
                    this.weekdayPeriod["周二"].period = this.formatPeriod(data["TUESDAY"])
                    this.weekdayPeriod["周二"].maxCount = this.getPropertyWithMaxValue(this.weekdayPeriod["周二"].period).property;
                    this.weekdayPeriod["周三"].period = this.formatPeriod(data["WEDNESDAY"])
                    this.weekdayPeriod["周三"].maxCount = this.getPropertyWithMaxValue(this.weekdayPeriod["周三"].period).property;
                    this.weekdayPeriod["周四"].period = this.formatPeriod(data["THURSDAY"])
                    this.weekdayPeriod["周四"].maxCount = this.getPropertyWithMaxValue(this.weekdayPeriod["周四"].period).property;
                    this.weekdayPeriod["周五"].period = this.formatPeriod(data["FRIDAY"])
                    this.weekdayPeriod["周五"].maxCount = this.getPropertyWithMaxValue(this.weekdayPeriod["周五"].period).property;
                    console.log(data, 'data');
                }
            })
        },
        getPropertyWithMaxValue(obj) {
            let maxProperty = null;
            let maxValue = -Infinity;
            for (let property in obj) {
                if (Object.prototype.hasOwnProperty.call(obj, property)) {
                    if (obj[property] > maxValue) {
                        maxValue = obj[property];
                        maxProperty = property;
                    }
                }
            }
            return {
                property: maxProperty, // 这里返回的是子属性名，如"11-13"
            };
        },
        // 转换时间段格式
        formatPeriod(obj) {
            const newObj = {};
            newObj["9:00-11:00"] = obj["9-11"];
            newObj["11:00-13:00"] = obj["11-13"];
            newObj["13:00-15:00"] = obj["13-15"];
            newObj["15:00-17:00"] = obj["15-17"];
            return newObj;
        },
    }
};
</script>


<style lang="scss" scoped>
.slider-container {
    width: 100%;
    height: 100vh;
    overflow: hidden;
    position: relative;
}

.slider {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    transition: transform 0.5s ease-in-out;
}

.slide {
    min-width: 100%;
    height: 100%;
    object-fit: cover;
}

.banner1 {
    background-image: url(../../../assets/banner5-2.png);
    padding: 240px 68px 0px 68px;
    color: white;
}

.date {
    font-size: 32px;
    letter-spacing: 10px;
    margin-bottom: 30px;
    font-weight: 700;
    font-family: "微软雅黑";

}

.week {
    font-size: 32px;
    letter-spacing: 50px;
    margin-bottom: 60px;
    font-family: "微软雅黑";

}

.line {
    height: 1px;
    width: 100%;
    margin-bottom: 60px;
    background-color: white;
}

.weekend {
    display: flex;
    justify-content: center;
}



.weeks {
    border-radius: 12px;
    margin: 0 80px;
    position: relative;
    width: 248px;
    height: 348px;
    z-index: 0;
    background-color: rgba(0, 0, 0, 0.2);
    font-family: "微软雅黑";

    >div {
        position: relative;
        z-index: 2;
        width: 248px;
        height: 348px;
        padding-top: 60px;
    }

    .weekday {
        font-size: 30px;
        margin-bottom: 14px;
        font-weight: 900;
    }

    .time {
        font-size: 18px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 28px;
        height: 56px;
        position: relative;
    }

    .bottom-line {
        border-bottom: 1px solid #978372;
    }

    .max-num {
        font-size: 30px;
        font-weight: 500;
    }

    .max {
        font-size: 24px;
        position: absolute;
        right: -12px;
        bottom: 18px;
    }
}

.weeks::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: -1;
    /* 确保伪元素在内容之下 */
    // filter: blur(50px);
    backdrop-filter: blur(5px);
    border-radius: 12px;

    /* 模糊伪元素 */

}

.odd {
    margin-top: 36px;
}
</style>